﻿var DataConvertor = new Object();
function Plane(ganttProject) {
    if(ganttProject)
    {
        this.Id = ganttProject.Id;
        this.Name = ganttProject.Name;
        this.ClientAction = ganttProject.ClientAction;
        this.ClientActionDate = ganttProject.ClientActionDate;
        this.Director = ganttProject.Director;
        if (ganttProject.StartDate) this.StartOn = ganttProject.StartDate.toEpoch();
        this.ChildTasks = [];
        this.ChangedTasks = [];
        this.TeamMembers = [];
    }
}
function Task(ganttTask) {
    if(ganttTask)
    {
        this.Id = ganttTask.Id;
        this.Name = ganttTask.Name;
        this.ClientAction = ganttTask.ClientAction;
        this.ClientActionDate = ganttTask.ClientActionDate;
        this.Director = ganttTask.Director;
        if (ganttTask.EST) this.StartOn = ganttTask.EST.toEpoch();
        this.Duration = ganttTask.Duration;
        this.PercentCompleted = ganttTask.PercentCompleted;
        this.ParentTaskId = ganttTask.ParentTaskId;
        this.DependencyTaskId = ganttTask.DependencyTaskId;
        this.ChildTasks = [];
    }
};

DataConvertor.ToServerPlane = function (ganttProjectInfo) {
    var serverPlane = new Plane(ganttProjectInfo);
    var i = 0;
    for (; i < ganttProjectInfo.ParentTasks.length; i++) {
        serverPlane.ChildTasks[i] = DataConvertor.ToServerTask(ganttProjectInfo.ParentTasks[i]);
    }
    for (i = 0; i < ganttProjectInfo.ChangedTasks.length; i++) {
        var changedServerTask = new Task(ganttProjectInfo.ChangedTasks[i]);
        serverPlane.ChangedTasks[i] = changedServerTask;
    }
    return serverPlane;
};

DataConvertor.ToServerTask = function (ganttTaskInfo) {
    var serverTask = new Task(ganttTaskInfo);
    var i = 0;
    for (i = 0; i < ganttTaskInfo.ChildTasks.length; i++) {
        serverTask.ChildTasks[i] = DataConvertor.ToServerTask(ganttTaskInfo.ChildTasks[i]);
    }
    return serverTask;
};

DataConvertor.ToGanttTaskInfo = function (task) {
    var ganttTaskInfo = new GanttTaskInfo(task.Id, task.Name, Date.fromEpoch(task.StartOn), task.Duration, task.PercentCompleted, task.DependencyTaskId, task.Director);
    var i = 0;
    for (i = 0; i < task.ChildTasks.length; i++) {
        ganttTaskInfo.addChildTask(DataConvertor.ToGanttTaskInfo(task.ChildTasks[i]));
    }
    return ganttTaskInfo;
};
DataConvertor.ToGanttProjectInfo = function (plane) {
    var ganttProjectInfo = new GanttProjectInfo(plane.Id, plane.Name, Date.fromEpoch(plane.StartOn), plane.Director);
    ganttProjectInfo.TeamMembers = plane.TeamMembers;
    var i = 0;
    for (i = 0; i < plane.ChildTasks.length; i++) {
        ganttProjectInfo.addTask(DataConvertor.ToGanttTaskInfo(plane.ChildTasks[i]));
    }
    return ganttProjectInfo;
};

GanttChart.prototype.loadDataIn = function(container, serverData) {
    this.initChartConfiguration();
    this.initData(serverData);
    this.create(container);
};
GanttChart.prototype.initChartConfiguration = function() {  
    this.setImagePath("imgs/");
    this.setEditable(true);
    this.showContextMenu(true);
    this.showDescTask(true, 'n,director');
    this.showDescProject(true, 'n,d,director');
};
GanttChart.prototype.initData = function (serverData) {
    this.clearData();
    if (serverData) {
        var i = 0;
        for (i = 0; i < serverData.Planes.length; i++) {
            this.addProject(DataConvertor.ToGanttProjectInfo(serverData.Planes[i]));
        }
    }
    this.TeamMembers = serverData.TeamMembers;
};
GanttChart.prototype.refresh = function () {
    var data = this.getServerData();
    this.initData(data);
    this.create(this.content.id);
};
GanttChart.prototype.getServerData = function() {
    var Project = {};
    Project.Planes = [];
    Project.Name = "";
    Project.Description = "";
    var i = 0;
    for (i = 0; i < this.Project.length; i++) {
        Project.Planes[i] = (DataConvertor.ToServerPlane(this.Project[i]));
    }
    return Project;
};
